package controller

import (
	// "fmt"
	"im/args"

	"im/service"
	"log"

	"github.com/gin-gonic/gin"
)

type userOperation struct {
}

func NewUserOperation() *userController {
	return &userController{}
}

func (uo *userController) StartInitUserOperation(contact *gin.RouterGroup) {
	contact.POST("/addfriend", uo.addFriend)
	contact.POST("/addgroup", uo.addGroup)
	contact.GET("/friend/:id", uo.getFriendInfo)
}

func (uo *userController) addFriend(c *gin.Context) {
	var arg args.ContactArgs
	if err:=c.ShouldBind(&arg); err != nil {

		c.String(200, err.Error())
		return 
	}
	log.Println(arg)
	if err := new(service.ContactService).AddFriend(arg); err != nil {
		c.String(200, err.Error())
		return 
	}
	c.String(200, "好友添加成功！")
	
}

func (uc *userController) getFriendInfo(c *gin.Context){
	id := c.Param("id")
	log.Printf("获得的id是：%s", id)
	service := new(service.ContactService)
	// 获取到用户好友ID的切片
	friendsIds, err := service.GetFriendIds(id)
	if err != nil {
		c.String(200, err.Error())
		return
	}
	// 循环查询数据库，将好友信息调出来，储存到切片中
	var friendInfoSlice []interface{}
	for _, friendId := range friendsIds {
		friendInfo, err := service.GetFriendInfo(friendId)
		if err != nil {
			c.String(200, err.Error())
			return
		}
		friendInfoSlice = append(friendInfoSlice, friendInfo)
	}

	// 返回所有的好友信息
	c.JSON(200,gin.H{
		"code":"200",
		"data":friendInfoSlice,
	})
	

}

func (uo *userController) addGroup(g *gin.Context) {

}

